import { defineComponent, reactive, ref, toRefs } from 'vue'
import {
  NInput,
  NForm,
  NFormItem,
  NModal,
  FormRules,
  NTreeSelect
} from 'naive-ui'
import { Response } from '@/service/modules/common/types'
import { useDepartment } from '@/views/department/tab/department-manage-tab/use-department'
import { createProjectReq } from '@/service/modules/project'

const CreateProjectModal = defineComponent({
  name: 'CreateProjectModal',
  props: {
    showModal: {
      type: Boolean,
      default: false
    }
  },
  emits: ['modalShowChange', 'refreshList'],
  setup() {
    const state = reactive({
      projectForm: {
        name: '',
        alias: '',
        description: '',
        departmentId: null
      },
      positiveButtonDisabled: ref(false),
      rules: {
        name: {
          trigger: ['input', 'blur'],
          required: true,
          validator() {
            if (state.projectForm.name === '') {
              return new Error(window.$t('project.nameTips'))
            }
          }
        },
        departmentId: {
          trigger: ['input', 'blur'],
          required: true,
          validator() {
            if (!state.projectForm.departmentId) {
              return new Error(window.$t('user.departmentTips'))
            }
          }
        }
      } as FormRules
    })
    //部门树下拉选项
    const { departmentVariables, getDepartmentOptions } = useDepartment()
    getDepartmentOptions()
    return {
      ...toRefs(state),
      departmentVariables
    }
  },
  methods: {
    async onPositiveClick() {
      //输入框存在为空时则确认按钮无效
      if (this.projectForm.name === '' || !this.projectForm.departmentId) {
        return
      }
      //提交创建项目请求
      this.positiveButtonDisabled = true
      const res: Response<any> = await createProjectReq({
        ...this.projectForm
      }).catch(() => {
        this.positiveButtonDisabled = false
      })
      if (res.resp_code === 0) {
        this.initData()
        this.$emit('modalShowChange', false)
        this.$emit('refreshList')
        this.positiveButtonDisabled = false
      } else {
        this.positiveButtonDisabled = false
      }
    },
    onNegativeClick() {
      this.initData()
      this.$emit('modalShowChange', false)
    },
    initData() {
      this.projectForm.name = ''
      this.projectForm.alias = ''
      this.projectForm.description = ''
      this.projectForm.departmentId = null
    }
  },
  render() {
    return (
      <NModal
        show={this.showModal}
        mask-closable={false}
        style='width: 400px'
        title={window.$t('project.createProject')}
        bordered={false}
        size='huge'
        preset='dialog'
        positive-text={window.$t('dialog.positiveText')}
        negative-text={window.$t('dialog.negativeText')}
        onPositiveClick={this.onPositiveClick}
        onNegativeClick={this.onNegativeClick}
        positiveButtonProps={{
          disabled:
            this.positiveButtonDisabled ||
            this.projectForm.name === '' ||
            !this.projectForm.departmentId
        }}
        onClose={this.onNegativeClick}
      >
        <NForm
          rules={this.rules}
          label-placement='left'
          require-mark-placement='right-hanging'
          label-width='auto'
        >
          <NFormItem path='name' label={window.$t('project.name')}>
            <NInput
              type='text'
              v-model={[this.projectForm.name, 'value']}
              placeholder={window.$t('project.nameTips')}
              autofocus
            />
          </NFormItem>
          <NFormItem path='alias' label={window.$t('project.alias')}>
            <NInput type='text' v-model={[this.projectForm.alias, 'value']} />
          </NFormItem>
          <NFormItem
            path='description'
            label={window.$t('project.description')}
          >
            <NInput
              type='textarea'
              v-model={[this.projectForm.description, 'value']}
            />
          </NFormItem>

          <NFormItem path='departmentId' label={window.$t('user.department')}>
            <NTreeSelect
              v-model={[this.projectForm.departmentId, 'value']}
              options={this.departmentVariables.departmentOptions}
              label-field='name'
              key-field='id'
              children-field='subs'
              clearable
              size='small'
              placeholder={window.$t('user.departmentTips')}
            />
          </NFormItem>
        </NForm>
      </NModal>
    )
  }
})

export default CreateProjectModal
